﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using WFNetLib.PacketProc;
using System.Threading;

namespace 生命体征测试仪
{    
    public partial class formMedical : formBase
    {
        CP1616Packet XueYaPacket;
        private bool XueYa_ComListening = false;//是否没有执行完invoke相关操作              
        private bool XueYa_ComClosing = false;//是否正在关闭串口，执行Application.DoEvents， 并阻止再次invoke 
        private void tabXueYa_Enter(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            if(SysConfig.XueYaCom!=null)
            {
                XueYaPacket = new CP1616Packet();
                SysConfig.XueYaCom.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(XueYaCom_DataReceived);
                XueYa_ComListening = false;
                XueYa_ComClosing = false;
            }
        }

        private void tabXueYa_Leave(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            if (SysConfig.XueYaCom != null)
            {
                XueYa_ComClosing = true;
                while (XueYa_ComListening)
                    Application.DoEvents();
                try
                {
                    SysConfig.XueYaCom.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(XueYaCom_DataReceived);
                }
                catch { }
            }
        }        
        private void XueYaCom_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            if (XueYa_ComClosing)
                return;
            XueYa_ComListening = true;
            int rxn = SysConfig.XueYaCom.BytesToRead;
            while (rxn != 0)
            {
                rxn--;
                byte rx = (byte)SysConfig.XueYaCom.ReadByte();
                if (XueYaPacket.DataPacketed(rx))
                {
                    try
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            lGaoYa.Text = XueYaPacket.Data[0].ToString();
                            lDiYa.Text = XueYaPacket.Data[1].ToString();
                            lXinLv.Text = XueYaPacket.Data[2].ToString();
                            MedicalResult.GaoYa = XueYaPacket.Data[0];
                            MedicalResult.DiYa = XueYaPacket.Data[1];
                            MedicalResult.XinLv = XueYaPacket.Data[2];
                            bChange = true;
                            byte[] ack = CP1616Packet.MakeCP1616Packet(0x01, 0x0001, null);
                            SysConfig.XueYaCom.Write(ack, 0, ack.Length);
                        }));
                    }
                    catch //(System.Exception ex)
                    {
                    	
                    }
                    XueYaPacket = new CP1616Packet();
                }
            }

            XueYa_ComListening = false;
        }
    }
}
